// Package account
// @Author zhongxc
// @Date 2024/7/30 9:55:00
// @Desc
package account

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
	accountLogicV1 "vben-api/pkg/logic/account.logic.v1"
	"vben-api/pkg/svc"
	common "vben-common"
)

type Account struct {
	svcCtx       *svc.ServiceContext
	accountLogic *accountLogicV1.AccountLogic
}

// NewAccount
// @Description:
// @param svcCtx
// @return *Account
func NewAccount(svcCtx *svc.ServiceContext) *Account {
	return &Account{
		svcCtx:       svcCtx,
		accountLogic: accountLogicV1.NewAccountLogic(svcCtx),
	}
}

// Login
// @Description: 登录
// @receiver a
// @param c
func (a *Account) Login(c *gin.Context) {
	r := common.NewResult()
	userName := c.PostForm("username")
	password := c.PostForm("password")
	if strings.TrimSpace(userName) == "" {
		c.JSON(http.StatusOK, r.Fail(400, "用户名不能为空"))
		return
	}
	if strings.TrimSpace(password) == "" {
		c.JSON(http.StatusOK, r.Fail(400, "密码不能为空"))
		return
	}

	result, err := a.accountLogic.Login(userName, password)
	if err != nil {
		c.JSON(http.StatusOK, r.Fail(400, err.Error()))
		return
	}
	c.JSON(http.StatusOK, r.Success(result))
}

// Logout
// @Description: 退出登录
// @receiver a
// @param c
func (a *Account) Logout(c *gin.Context) {
	r := common.NewResult()
	c.JSON(http.StatusOK, r.Success(a.accountLogic.Logout(c.Request)))
}

// RefreshToken
// @Description: 刷新token
// @receiver a
// @param c
func (a *Account) RefreshToken(c *gin.Context) {
	r := common.NewResult()
	c.JSON(http.StatusOK, r.Success(nil))
}

// Profile
// @Description: 获取用户信息
// @receiver a
// @param c
func (a *Account) Profile(c *gin.Context) {
	r := common.NewResult()
	// userInfo := c.MustGet("userinfo")
	// if userInfo != nil {
	// 	user := userInfo.(*util.AccessDetails)
	// 	c.JSON(http.StatusOK, r.Success(user))
	// 	return
	// }

	// userInfo := a.svcCtx.GetUserInfo()
	// if userInfo != nil {
	// 	user := userInfo.(*util.AccessDetails)
	// 	c.JSON(http.StatusOK, r.Success(user))
	// 	return
	// }

	data := map[string]interface{}{
		"userId":   a.svcCtx.GetUserInfo().UserId,
		"username": a.svcCtx.GetUserInfo().UserName,
		"nickname": "超级管理员",
		"avatar":   "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
		"roles": []map[string]any{
			{
				"value":    "super",
				"roleName": "超级管理员",
			},
		},
	}

	c.JSON(http.StatusOK, r.Success(data))
}
